package com.apache.common.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.client.UctCoreClient;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * 校验注册用户是否唯一
 *
 */
public class UniqueUctUserOnlyPluginImpl implements CustomMethodPlugin {
    protected Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        //		String userEname = StrUtil.doNull(String.valueOf(param.get("member.memberEname")),
        //				String.valueOf(param.get("uctUser.userEname")));
        String userEname = String.valueOf(param.get("uctUser.userEname"));
        log.info("校验用户唯一性开始："+userEname);
        if (Validator.isEmpty(userEname)) {
            throw new BusinessException("缺少参数");
        } else {
            //校验是否重复
            ResultEntity re = UctCoreClient.getUserByProperty("userEname", userEname);
            if (!Validator.isEmpty(re.getEntity()) || re.getResult().equals("false")) {
                throw new BusinessException("用户已存在！");
            }
        }
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }
}
